package com.ruoyi.zdgl.service.impl;

import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zdgl.domain.YwDdxxb;
import com.ruoyi.zdgl.domain.YwDdxxbExport;
import com.ruoyi.zdgl.domain.YwDdxxbVo;
import com.ruoyi.zdgl.mapper.YwDdxxbMapper;
import com.ruoyi.zdgl.service.IYwDdxxbService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单信息Service业务层处理
 *
 * @author chrui
 * @date 2024-07-27
 */
@Service
public class YwDdxxbServiceImpl implements IYwDdxxbService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private YwDdxxbMapper ywDdxxbMapper;

    /**
     * 查询订单信息
     *
     * @param ddbh 订单信息主键
     * @return 订单信息
     */
    @Override
    public YwDdxxb selectYwDdxxbByDdbh(Long ddbh) {
        return ywDdxxbMapper.selectYwDdxxbByDdbh(ddbh);
    }

    /**
     * 查询订单信息列表
     *
     * @param ywDdxxb 订单信息
     * @return 订单信息
     */
    @Override
    public List<YwDdxxb> selectYwDdxxbList(YwDdxxb ywDdxxb) {
        return ywDdxxbMapper.selectYwDdxxbList(ywDdxxb);
    }

    /**
     * 导出
     *
     * @param ywDdxxb 订单信息
     * @return
     */
    @Override
    public List<YwDdxxbExport> selectYwDdxxbListExport(YwDdxxb ywDdxxb) {
        return ywDdxxbMapper.selectYwDdxxbListExport(ywDdxxb);
    }

    @Override
    public Map<String, Object> selectYwDdxxbListCount(YwDdxxb ywDdxxb) {
        return ywDdxxbMapper.selectYwDdxxbListCount(ywDdxxb);
    }

    /**
     * 新增订单信息
     *
     * @param ywDdxxb 订单信息
     * @return 结果
     */
    @Override
    public int insertYwDdxxb(YwDdxxb ywDdxxb) {
        ywDdxxb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        ywDdxxb.setCreateTime(DateUtils.getNowDate());
        YwDdxxb ywDdxxb1 = ywDdxxbMapper.selectYwDdxxbByMcAnBj(ywDdxxb.getMcjgg(), ywDdxxb.getSjbh());
        if (ywDdxxb1 != null) {
            throw new UtilException("名称及规格：" + ywDdxxb.getMcjgg() + "，" + "所属收据编号：" + ywDdxxb.getSjbh() + "，" + "已存在");
        }
        //判断  收据编号 是不是有了，如果有，判断是不是这个项目和这个公司的，如果不是，就提示
        if (StringUtils.isNotEmpty(ywDdxxb.getSjbh())) {
            List<YwDdxxb> ywDdxxbList = ywDdxxbMapper.selectYwDdxxbBySjbh(ywDdxxb.getSjbh());
            if (ywDdxxbList != null && ywDdxxbList.size() > 0) {
                YwDdxxb ywDdxxbcc = ywDdxxbList.get(0);
                if (ywDdxxbcc != null) {
                    Long gsbh = ywDdxxbcc.getGsbh();
                    Long xmbh = ywDdxxbcc.getXmbh();
                    if (xmbh != null && xmbh.longValue() != ywDdxxb.getXmbh().longValue()) {
                        throw new UtilException("收据编号：" + ywDdxxb.getSjbh() + "，" + "所属项目已存在，不能一个收据编号多个项目，请查询！");
                    }
                    if (gsbh != null && gsbh.longValue() != ywDdxxb.getGsbh().longValue()) {
                        throw new UtilException("收据编号：" + ywDdxxb.getSjbh() + "，" + "所属公司已存在，不能一个收据编号多个项目，请查询！");
                    }
                }
            }
        }
        return ywDdxxbMapper.insertYwDdxxb(ywDdxxb);
    }

    /**
     * 新增订单信息 批量文本
     *
     * @param ywDdxxbVo 订单信息
     * @return 结果
     */
    @Override
    public int addDdxxbByText(YwDdxxbVo ywDdxxbVo) {


        String theText = "";//名称及规格	单位  数量  单价  金额
        if (StringUtils.isNotEmpty(ywDdxxbVo.getSjnr())) {
            theText = ywDdxxbVo.getSjnr();
        } else {
            throw new UtilException("数据内容不能为空");
        }
        logger.info(theText);
        YwDdxxb ywDdxxb = new YwDdxxb();
        ywDdxxb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        ywDdxxb.setCreateTime(DateUtils.getNowDate());
        ywDdxxb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        ywDdxxb.setXmbh(ywDdxxbVo.getXmbh());
        ywDdxxb.setRq(ywDdxxbVo.getRq());
        ywDdxxb.setZdr(ywDdxxbVo.getZdr());
        ywDdxxb.setSjbh(ywDdxxbVo.getSjbh());
        ywDdxxb.setGsbh(ywDdxxbVo.getGsbh());
        //判断  收据编号 是不是有了，如果有，判断是不是这个项目和这个公司的，如果不是，就提示
        if (StringUtils.isNotEmpty(ywDdxxb.getSjbh())) {
            List<YwDdxxb> ywDdxxbList = ywDdxxbMapper.selectYwDdxxbBySjbh(ywDdxxb.getSjbh());
            if (ywDdxxbList != null && ywDdxxbList.size() > 0) {
                YwDdxxb ywDdxxbcc = ywDdxxbList.get(0);
                if (ywDdxxbcc != null) {
                    Long gsbh = ywDdxxbcc.getGsbh();
                    Long xmbh = ywDdxxbcc.getXmbh();
                    if (xmbh != null && xmbh.longValue() != ywDdxxb.getXmbh().longValue()) {
                        throw new UtilException("收据编号：" + ywDdxxb.getSjbh() + "，" + "所属项目已存在，不能一个收据编号多个项目，请查询！");
                    }
                    if (gsbh != null && gsbh.longValue() != ywDdxxb.getGsbh().longValue()) {
                        throw new UtilException("收据编号：" + ywDdxxb.getSjbh() + "，" + "所属公司已存在，不能一个收据编号多个项目，请查询！");
                    }
                }
            }
        }

        String[] textArray = StringUtils.split(theText, "\n");
        int tt = 0;
        if (StringUtils.isNotEmpty(textArray)) {
            for (int i = 0; i < textArray.length; i++) {
                tt++;
                String[] textArray2 = StringUtils.split(textArray[i], "\t");
                if (StringUtils.isNotEmpty(textArray2)) {
                    int length = textArray2.length;
                    if (length < 5) {
                        throw new UtilException("第" + tt + "行，数据内容的列数应为5列，分别是【名称及规格\t单位\t数量\t单价\t金额】");
                    }
                    String mcjgg = textArray2[0];
                    if (StringUtils.isEmpty(mcjgg)) {
                        throw new UtilException("第" + tt + "行，名称及规格列未填写");
                    }
                    ywDdxxb.setMcjgg(StringUtils.replaceAll(mcjgg, " ", ""));
                    String dw = textArray2[1];
                    ywDdxxb.setDw(StringUtils.replaceAll(dw, " ", ""));
                    BigDecimal sl = new BigDecimal("0");
                    try {
                        sl = new BigDecimal(textArray2[2]);
                    } catch (Exception e) {
                        throw new UtilException("第" + tt + "行，数量，应该为数字");
                    }
                    ywDdxxb.setSl(sl);
                    BigDecimal dj = new BigDecimal("0");
                    ;
                    try {
                        dj = new BigDecimal(textArray2[3]);
                    } catch (Exception e) {
                        throw new UtilException("第" + tt + "行，单价，应该为数字");
                    }
                    ywDdxxb.setDj(dj);
                    BigDecimal zj = new BigDecimal("0");
                    try {
                        zj = new BigDecimal(textArray2[4]);
                    } catch (Exception e) {
                        throw new UtilException("第" + tt + "行，金额总价，应该为数字");
                    }
                    ywDdxxb.setZj(zj);
                    //收据编号 + 名称  是唯一的？？
                    YwDdxxb ywDdxxb1 = ywDdxxbMapper.selectYwDdxxbByMcAnBj(ywDdxxb.getMcjgg(), ywDdxxb.getSjbh());
                    if (ywDdxxb1 != null) {
                        ywDdxxb.setDdbh(ywDdxxb1.getDdbh());
                        ywDdxxb.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
                        ywDdxxb.setUpdateTime(DateUtils.getNowDate());
                        ywDdxxbMapper.updateYwDdxxb(ywDdxxb);
                        return 1;
                    }
                    ywDdxxbMapper.insertYwDdxxb(ywDdxxb);
                }
            }
        }
        return 1;
    }

    @Override
    public String checkDdxxbByText(YwDdxxbVo ywDdxxbVo) {
        Map<String, Object>  errorMap = new HashMap<>();
        String checkMsg = "";
        StringBuffer sb = new StringBuffer();
        String theText = "";//名称及规格	单位  数量  单价  金额
        if (StringUtils.isNotEmpty(ywDdxxbVo.getSjnr())) {
            theText = ywDdxxbVo.getSjnr();
        } else {
            throw new UtilException("数据内容不能为空");
        }
        logger.info(theText);
        YwDdxxb ywDdxxb = new YwDdxxb();
        ywDdxxb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        ywDdxxb.setCreateTime(DateUtils.getNowDate());
        ywDdxxb.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        ywDdxxb.setXmbh(ywDdxxbVo.getXmbh());
        ywDdxxb.setRq(ywDdxxbVo.getRq());
        ywDdxxb.setZdr(ywDdxxbVo.getZdr());
        ywDdxxb.setSjbh(ywDdxxbVo.getSjbh());
        ywDdxxb.setGsbh(ywDdxxbVo.getGsbh());
        //判断  收据编号 是不是有了，如果有，判断是不是这个项目和这个公司的，如果不是，就提示
        if (StringUtils.isNotEmpty(ywDdxxb.getSjbh())) {
            List<YwDdxxb> ywDdxxbList = ywDdxxbMapper.selectYwDdxxbBySjbh(ywDdxxb.getSjbh());
            if (ywDdxxbList != null && ywDdxxbList.size() > 0) {
                YwDdxxb ywDdxxbcc = ywDdxxbList.get(0);
                if (ywDdxxbcc != null) {
                    Long gsbh = ywDdxxbcc.getGsbh();
                    Long xmbh = ywDdxxbcc.getXmbh();
                    if (xmbh != null && xmbh.longValue() != ywDdxxb.getXmbh().longValue()) {
                        throw new UtilException("收据编号：" + ywDdxxb.getSjbh() + "，" + "所属项目已存在，不能一个收据编号多个项目，请查询！");
                    }
                    if (gsbh != null && gsbh.longValue() != ywDdxxb.getGsbh().longValue()) {
                        throw new UtilException("收据编号：" + ywDdxxb.getSjbh() + "，" + "所属公司已存在，不能一个收据编号多个项目，请查询！");
                    }
                }
            }
        }
        String[] textArray = StringUtils.split(theText, "\n");
        int tt = 0;
        if (StringUtils.isNotEmpty(textArray)) {
            for (String s : textArray) {
                tt++;
                String[] textArray2 = StringUtils.split(s, "\t");
                if (StringUtils.isNotEmpty(textArray2)) {
                    int length = textArray2.length;
                    if (length < 5) {
                        throw new UtilException("第" + tt + "行，数据内容的列数应为5列，分别是【名称及规格\t单位\t数量\t单价\t金额】");
                    }
                    String mcjgg = textArray2[0];
                    if (StringUtils.isEmpty(mcjgg)) {
                        throw new UtilException("第" + tt + "行，名称及规格列未填写");
                    }
                    BigDecimal sl = new BigDecimal("0");
                    try {
                        sl = new BigDecimal(textArray2[2]);
                    } catch (Exception e) {
                        throw new UtilException("第" + tt + "行，数量，应该为数字");
                    }
                    BigDecimal dj = new BigDecimal("0");
                    try {
                        dj = new BigDecimal(textArray2[3]);
                    } catch (Exception e) {
                        throw new UtilException("第" + tt + "行，单价，应该为数字");
                    }
                    BigDecimal zj = new BigDecimal("0");
                    try {
                        zj = new BigDecimal(textArray2[4]);
                    } catch (Exception e) {
                        throw new UtilException("第" + tt + "行，金额总价，应该为数字");
                    }
                    BigDecimal zjPerm = NumberUtil.mul(dj, sl);
                    BigDecimal zjJs = new BigDecimal("0");
                    String dictValue = DictUtils.getDictValue("jesfsswr", "jesfsswr");
                    if (StringUtils.isNotEmpty(dictValue)) {
                        if (dictValue.equals("1")) {
                            zjJs = NumberUtil.roundHalfEven(zjPerm, 2);
                        } else {
                            zjJs = zjPerm;
                        }
                    } else {
                        zjJs = zjPerm;
                    }
                    //判断金额总价是否等于数量*单价
                    if (zj.compareTo(zjJs) != 0) {
                        String message = "第" + tt + "行，【" + mcjgg + "】，金额总价与数量和单价相乘不相等，请检查！";
                        sb.append(message).append("；\n");
                    }


                }
            }
        }
        if (sb != null && sb.length() > 0) {
            checkMsg = sb.toString();
        }
        return checkMsg;
    }


    /**
     * 修改订单信息
     *
     * @param ywDdxxb 订单信息
     * @return 结果
     */
    @Override
    public int updateYwDdxxb(YwDdxxb ywDdxxb) {
        ywDdxxb.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        ywDdxxb.setUpdateTime(DateUtils.getNowDate());
        return ywDdxxbMapper.updateYwDdxxb(ywDdxxb);
    }

    /**
     * 批量删除订单信息
     *
     * @param ddbhs 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteYwDdxxbByDdbhs(Long[] ddbhs) {
        return ywDdxxbMapper.deleteYwDdxxbByDdbhs(ddbhs);
    }

    /**
     * 删除订单信息信息
     *
     * @param ddbh 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteYwDdxxbByDdbh(Long ddbh) {
        return ywDdxxbMapper.deleteYwDdxxbByDdbh(ddbh);
    }

    @Override
    public YwDdxxb updateDdxxbByColumn(YwDdxxb ywDdxxb) {
        BigDecimal dj = ywDdxxb.getDj();
        BigDecimal sl = ywDdxxb.getSl();
        BigDecimal zero = new BigDecimal("0");
        if(dj==null||sl==null){
            throw new UtilException("单价和数量未填写");
        }
        if (dj.compareTo(zero) <= 0) {
            throw new UtilException("单价应该大于0");
        }
        if (sl.compareTo(zero) <= 0) {
            throw new UtilException("数量应该大于0");
        }
        BigDecimal zj = new BigDecimal("0");
        BigDecimal zjPerm =  NumberUtil.mul(dj, sl);
        String dictValue = DictUtils.getDictValue("jesfsswr", "jesfsswr");
        if(StringUtils.isNotEmpty(dictValue)){
            if(dictValue.equals("1")){
                zj = NumberUtil.roundHalfEven(zjPerm, 2);
            }else{
                zj = zjPerm;
            }
        }else{
            zj = zjPerm;
        }
        ywDdxxb.setZj(zj);
        YwDdxxb d = new YwDdxxb();
        d.setDdbh(ywDdxxb.getDdbh());
        d.setDj(ywDdxxb.getDj());
        d.setSl(ywDdxxb.getSl());
        d.setZj(ywDdxxb.getZj());
        ywDdxxbMapper.updateYwDdxxb(d);
        return ywDdxxb;
    }
}
